package top.huxy.boot.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import top.huxy.boot.annotation.Master;
import top.huxy.boot.annotation.Operation;
import top.huxy.boot.annotation.RequestRemake;
import top.huxy.boot.model.dto.DTOBlog;
import top.huxy.boot.model.dto.Pager;
import top.huxy.boot.model.entity.Admin;
import top.huxy.boot.model.vo.VOBlog;
import top.huxy.boot.model.vo.VOBlogDetails;
import top.huxy.boot.service.BlogService;
import top.huxy.boot.util.obj.Result;

import java.util.List;

@RestController("adminBlogController")
@RequestMapping("/admin/blog")
public class BlogController {

    BlogService blogService;

    public BlogController(BlogService blogService) {
        this.blogService = blogService;
    }

    @Operation("添加博客")
    @PostMapping("/releaseBlog")
    @Master(value = Master.Role.NORMAL_ADMIN, msg = "无管理员权限")
    public Result releaseBlog(DTOBlog dtoBlog, Admin admin) {
        Integer id = blogService.addBlog(dtoBlog, admin.getId());

        return Result.success(id);
    }

    @GetMapping("/getBlogDetail")
    @RequestRemake("获取博客详情")
    public Result getBlogDetail(@RequestParam("id") Integer id) {
        VOBlogDetails voBlogDetails = blogService.getBlogDetailById(id);
        return Result.success(voBlogDetails);
    }

    @Operation("修改博客")
    @PostMapping("/updateBlog")
    @Master(Master.Role.NORMAL_ADMIN)
    public Result updateBlog(DTOBlog dtoBlog, Admin admin) {
        blogService.updateBlog(dtoBlog, admin.getId());
        return Result.success("ok");
    }

    @GetMapping("/getBlogList")
    @RequestRemake("管理员获取博客列表")
    public Result getBlogList(Pager pager) {
        //准备分页
        PageHelper.startPage(pager.getPage(), pager.getPageSize(), "create_time desc");

        //查询数据
        List<VOBlog> blogList = blogService.getBlogList(pager);

        //封装数据
        PageInfo<VOBlog> blogPageInfo = new PageInfo<>(blogList);

        return Result.success(blogPageInfo);
    }
}
